using System;
using System.Globalization;

using System.Data;


using WP.MF.DataEntity;
using WP.Commons;

namespace WP.MF.BusinessRule
{
	/// <summary>
	/// Summary description for MPeriod.BusinessRule.
	/// </summary>
	public class MPeriodBRL
	{
		public MPeriodBRL()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		public bool InsertMPeriodValid(MPeriodDEL mPeriodDEL)
		{
			bool bReturn=true;
			string strModuleName="";
			int iMaxIndex=0;
			string strMessage="";
			DateTime dtStartDate;
			DateTime dtEndDate;
			DateTime dtOldEndDate;
			DataRow DR=null;

			SysVar.ErrorMessage.Clear();
			strModuleName=mPeriodDEL.Tables[MPeriodDEL.TABLENAME_MPERIOD].Rows[0][MPeriodDEL.MODULENAME].ToString();

			if(strModuleName=="GL")
				iMaxIndex=24;
			else
				iMaxIndex=13;

			for(int i=2;i<=iMaxIndex;i++)
			{
				DR=mPeriodDEL.Tables[MPeriodDEL.TABLENAME_MPERIOD].Rows[i-1];
//				if(DR[MPeriodDEL.PERIOD].ToString().ToUpper()!="CP")
//				{
					dtStartDate=Convert.ToDateTime(DR[MPeriodDEL.STPRDDATE].ToString());
					dtEndDate=Convert.ToDateTime(DR[MPeriodDEL.ENPRDDATE].ToString());
					if(i>2)
					{
						dtOldEndDate=Convert.ToDateTime(mPeriodDEL.Tables[MPeriodDEL.TABLENAME_MPERIOD].Rows[i-2][MPeriodDEL.ENPRDDATE].ToString());
						if(dtStartDate!=dtOldEndDate.AddDays(1))
						{
							//strMessage="Current year starting date("+DR[MPeriodDEL.STPRDDATE].ToString()+") and  ending date("+dtOldEndDate.ToString("d",DateTimeFormatInfo.InvariantInfo)+") are not consecutive!";
                            strMessage = DataFunctions.GetMsg(this.GetType().Name, "MSG_StartDateAndEndDate", new string[] { DR[MPeriodDEL.STPRDDATE].ToString(), dtOldEndDate.ToString("d", DateTimeFormatInfo.InvariantInfo) });
							SysVar.ErrorMessage.Add(strMessage);
							bReturn=false;
						}
					}
				
					if(dtEndDate<dtStartDate)
					{
						//strMessage="Current year starting date("+DR[MPeriodDEL.STPRDDATE].ToString()+") can not be greater than ending date("+DR[MPeriodDEL.ENPRDDATE].ToString()+")!";
                        strMessage = DataFunctions.GetMsg(this.GetType().Name, "MSG_CurrentYearStartDate", new string[] { DR[MPeriodDEL.STPRDDATE].ToString(), DR[MPeriodDEL.ENPRDDATE].ToString() });
                        SysVar.ErrorMessage.Add(strMessage);
						bReturn=false;
					}
					if(i<=12)
					{
						dtStartDate=Convert.ToDateTime(DR[MPeriodDEL.PREYRSTPRDDATE].ToString());
						dtEndDate=Convert.ToDateTime(DR[MPeriodDEL.PREYRENPRDDATE].ToString());	
						if(dtEndDate<dtStartDate)
						{
							//strMessage="Previous year starting date("+DR[MPeriodDEL.PREYRSTPRDDATE].ToString()+") can not be greater than ending date("+DR[MPeriodDEL.PREYRENPRDDATE].ToString()+")!";
                            strMessage = DataFunctions.GetMsg(this.GetType().Name, "MSG_PreviousYearStartDate", new string[] { DR[MPeriodDEL.PREYRSTPRDDATE].ToString(), DR[MPeriodDEL.PREYRENPRDDATE].ToString() });
							SysVar.ErrorMessage.Add(strMessage);
							bReturn=false;
						}

						if(i>2)
						{
							dtOldEndDate=Convert.ToDateTime(mPeriodDEL.Tables[MPeriodDEL.TABLENAME_MPERIOD].Rows[i-2][MPeriodDEL.PREYRENPRDDATE].ToString());
							if(dtStartDate!=dtOldEndDate.AddDays(1))
							{
								//strMessage="Previous year starting date("+dtStartDate.ToString("d",DateTimeFormatInfo.InvariantInfo)+") and ending date("+dtOldEndDate.ToString("d",DateTimeFormatInfo.InvariantInfo)+") are not consecutive!";
                                strMessage = DataFunctions.GetMsg(this.GetType().Name, "MSG_PreYearStartDateAndEndDate", new string[] { dtStartDate.ToString("d", DateTimeFormatInfo.InvariantInfo), dtOldEndDate.ToString("d", DateTimeFormatInfo.InvariantInfo) });
								SysVar.ErrorMessage.Add(strMessage);
								bReturn=false;
							}
						}

						dtStartDate=Convert.ToDateTime(DR[MPeriodDEL.PREPREYRSTPRDDATE].ToString());
						dtEndDate=Convert.ToDateTime(DR[MPeriodDEL.PREPREYRENPRDDATE].ToString());	
						if(dtEndDate<dtStartDate)
						{
							//strMessage="Pre previous year starting date("+DR[MPeriodDEL.PREPREYRSTPRDDATE].ToString()+") can not be greater than ending date("+DR[MPeriodDEL.PREPREYRENPRDDATE].ToString()+")!";
                            strMessage = DataFunctions.GetMsg(this.GetType().Name, "MSG_PreviousYearStartDate", new string[] { DR[MPeriodDEL.PREPREYRSTPRDDATE].ToString(), DR[MPeriodDEL.PREPREYRENPRDDATE].ToString() });
							SysVar.ErrorMessage.Add(strMessage);
							bReturn=false;
						}

						if(i>2)
						{
							dtOldEndDate=Convert.ToDateTime(mPeriodDEL.Tables[MPeriodDEL.TABLENAME_MPERIOD].Rows[i-2][MPeriodDEL.PREPREYRENPRDDATE].ToString());
							if(dtStartDate!=dtOldEndDate.AddDays(1))
							{
								//strMessage="Pre previous year starting date("+dtStartDate.ToString("d",DateTimeFormatInfo.InvariantInfo)+") and ending date("+dtOldEndDate.ToString("d",DateTimeFormatInfo.InvariantInfo)+") are not consecutive!";
                                strMessage = DataFunctions.GetMsg(this.GetType().Name, "MSG_PreYearStartDateAndEndDate", new string[] { dtStartDate.ToString("d", DateTimeFormatInfo.InvariantInfo), dtOldEndDate.ToString("d", DateTimeFormatInfo.InvariantInfo) });
								SysVar.ErrorMessage.Add(strMessage);
								bReturn=false;
							}
						}

					}
//				}
			}
			
			return bReturn;
		}

	}
}
